package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.enums.NotificationTypeEnum;
import com.suray.wcs.service.system.entity.CodeDB;
import com.suray.wcs.service.system.entity.NotificationDB;
import com.suray.wcs.service.system.mapper.NotificationDBMapper;
import com.suray.wcs.service.system.service.INotificationService;
import com.suray.wcs.service.util.ExcelUtilPlus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/notice")
@Api("消息通知")
public class NotificationController extends BaseController {

    @Autowired
    private INotificationService INotificationService;
    @Autowired
    private NotificationDBMapper notificationDBMapper;

    /**
     * 获取通知公告列表
     */
    @ApiOperation("获取通知公告列表")
    @PreAuthorize("@ss.hasPermi('notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(NotificationDB notice) {
        startPage();
        List<NotificationDB> list = INotificationService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @ApiOperation("导出通知公告列表")
    @Log(title = "消息通知", method = "导出消息通知", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('notice:export')")
    @GetMapping("/export")
    public ResponseData export(NotificationDB notice) {
        List<NotificationDB> list = INotificationService.selectNoticeList(notice);
        ExcelUtilPlus<NotificationDB> util = new ExcelUtilPlus<NotificationDB>(NotificationDB.class);
        return util.exportExcel(list, "消息通知数据");
    }


    /**
     * 获取通知公告列表
     */
    @ApiOperation("获取通知公告列表")
    @GetMapping("/getList")
    public ResponseData getList(NotificationDB notice) {
        startPage();
        notice.setIsRead(false);
        List<NotificationDB> list = INotificationService.selectNoticeList(notice);
        HashMap<String, Object> resMap = new HashMap<>();
        int infoTotal = INotificationService.selectNoticeCount(NotificationTypeEnum.INFO.getType());
        int warnTotal = INotificationService.selectNoticeCount(NotificationTypeEnum.WARN.getType());
        int errorTotal = INotificationService.selectNoticeCount(NotificationTypeEnum.ERROR.getType());
        resMap.put("infoTotal", infoTotal);
        resMap.put("warnTotal", warnTotal);
        resMap.put("errorTotal", errorTotal);
        resMap.put("data", list);
        return ResponseData.success(resMap);
    }


    @Log(title = "消息通知", method = "设置已读", businessType = BusinessType.UPDATE)
    @PutMapping("/readNotification")
    @ApiOperation("设置已读")
    public ResponseData readNotification(@RequestBody List<Long> notificationIds) {
        INotificationService.readNotification(notificationIds);
        return ResponseData.success();
    }

    @Log(title = "消息通知", method = "设置全部已读", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('notice:edit')")
    @PutMapping("/readAllNotification")
    @ApiOperation("设置全部已读")
    public ResponseData readAllNotification() {
        INotificationService.readAllNotification();
        return ResponseData.success();
    }

    @Log(title = "消息通知", method = "删除消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public ResponseData remove(@PathVariable Long[] infoIds) {
        return toAjax(notificationDBMapper.deleteBatchIds(Arrays.asList(infoIds)));
    }
}
